import baostock as bs
import pandas as pd
 
# 登录系统
lg = bs.login()
 
# 获取沪深A股历史K线数据
fields = "date,code,open,high,low,close,preclose,volume,amount,adjustflag"
df = bs.query_history_k_data("sh.600519", fields, start_date='2020-01-01', end_date='2020-12-31', frequency="d", adjustflag="3")
 
data_list = []
while (df.error_code == '0') & df.next():
    data_list.append(df.get_row_data())
 
result = pd.DataFrame(data_list, columns=df.fields)
result.to_csv('test.csv', encoding='GB18030')
bs.logout()
print(result)
